package com.alibaba.citrus.ots.platform.enhance.functions.diff.activity.router;

import com.alibaba.citrus.ots.platform.enhance.facade.service.diff.common.DiffCheckRequest;
import com.alibaba.citrus.ots.platform.enhance.functions.diff.common.constant.DiffStrategyRouteDict;
import com.alibaba.citrus.ots.platform.enhance.functions.diff.common.constant.SalesOrderLineDiffStandardFieldDict;
import com.alibaba.citrus.ots.platform.enhance.functions.diff.common.router.DiffCheckProcessor;
import com.alibaba.citrus.ots.platform.enhance.functions.diff.common.template.OneDiffOneTemplate;
import com.alibaba.citrus.ots.platform.enhance.repository.model.diff.common.FieldLineDTO;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Map;

/**
 * @author: qiaojiange
 * @create: 2022/01/19
 * @Description sap015主单（基准）和 销售主单 之间的对账
 */
@Component
public class Sap015DetailDiffSalesOrderLineProcessor extends DiffCheckProcessor {

    private Log log = Log.getLogger(Sap015DetailDiffSalesOrderLineProcessor.class);


    @Resource
    private OneDiffOneTemplate oneDiffOneTemplate;

    @Override
    public String obtainStrategyRoute() {
        return DiffStrategyRouteDict.SAP015DETAIL_DIFF_SALES_ORDER_LINE.getStrategyCode();
    }

    @Override
    public Result<String> diff(DiffCheckRequest diffCheckRequest) {

        //sap015和销售子单对账，需要处理数据，比如货品的内外码转换，金额*100的问题
        //这里单独的使用每一个字段进行对账
        Map<String, FieldLineDTO> baseLineDataMap = diffCheckRequest.getBaseLineDataMap();
        Map<String, FieldLineDTO> salesSubOrderDiffDataMap = diffCheckRequest.getDiffDataMap();


        //字段处理
        detailDBFieldValue(baseLineDataMap);


        return super.diff(diffCheckRequest, oneDiffOneTemplate);
    }


    public void detailDBFieldValue(Map<String, FieldLineDTO> dto) {

        //货品单价金额处理，
        FieldLineDTO unitPriceDTO = dto.get(SalesOrderLineDiffStandardFieldDict
                .UNIT_PRICE.getFieldCode());
        String unitPriceValue = unitPriceDTO.getFieldValue();
        if (StringUtils.isNotBlank(unitPriceValue)) {
            unitPriceValue = multiply(unitPriceValue,100);

            unitPriceDTO = FieldLineDTO.of("unitPrice", SalesOrderLineDiffStandardFieldDict.UNIT_PRICE.getFieldCode(), unitPriceValue);
            dto.put(SalesOrderLineDiffStandardFieldDict.UNIT_PRICE.getFieldCode(), unitPriceDTO);
        }

        //数量处理,去小数点
        FieldLineDTO quantityDTO = dto.get(SalesOrderLineDiffStandardFieldDict
                .ITEM_QUANTITY.getFieldCode());
        String quantityValue = quantityDTO.getFieldValue();
        if (StringUtils.isNotBlank(quantityValue)) {
            quantityValue = multiply(quantityValue,1);

            quantityDTO = FieldLineDTO.of("quantity", SalesOrderLineDiffStandardFieldDict.ITEM_QUANTITY.getFieldCode(), quantityValue);
            dto.put(SalesOrderLineDiffStandardFieldDict.ITEM_QUANTITY.getFieldCode(), quantityDTO);
        }

        //活动单价金额处理
        FieldLineDTO specialOfferPriceDTO = dto.get(SalesOrderLineDiffStandardFieldDict
                .SPECIAL_OFFER_PRICE.getFieldCode());
        String specialOfferPriceValue = specialOfferPriceDTO.getFieldValue();
        if (StringUtils.isNotBlank(specialOfferPriceValue)) {
            specialOfferPriceValue = multiply(specialOfferPriceValue,100);

            specialOfferPriceDTO = FieldLineDTO.of("activityPrice", SalesOrderLineDiffStandardFieldDict.SPECIAL_OFFER_PRICE.getFieldCode(), specialOfferPriceValue);
            dto.put(SalesOrderLineDiffStandardFieldDict.SPECIAL_OFFER_PRICE.getFieldCode(), specialOfferPriceDTO);
        }

        //总金额处理
        FieldLineDTO subTotalAmountDTO = dto.get(SalesOrderLineDiffStandardFieldDict
                .SUB_TOTAL_AMOUNT.getFieldCode());

        String subTotalAmountValue = subTotalAmountDTO.getFieldValue();
        if (StringUtils.isNotBlank(subTotalAmountValue)) {
            subTotalAmountValue = multiply(subTotalAmountValue,100);
            subTotalAmountDTO = FieldLineDTO.of("shouldPayFee", SalesOrderLineDiffStandardFieldDict.SUB_TOTAL_AMOUNT.getFieldCode(), subTotalAmountValue);
            dto.put(SalesOrderLineDiffStandardFieldDict.SUB_TOTAL_AMOUNT.getFieldCode(), subTotalAmountDTO);
        }

    }

    /**
     * 金额元转分
     *
     * @param sAmount
     * @return
     */
    public String multiply(String sAmount,Integer mul) {

        if (StringUtils.isBlank(sAmount) || StringUtils.isBlank(sAmount.trim())) {
            return null;
        }
        sAmount = sAmount.trim();
        BigDecimal amount = new BigDecimal(sAmount);
        BigDecimal multiply = new BigDecimal(mul);
        return amount.multiply(multiply).setScale(0, RoundingMode.DOWN).toString();
    }
}
